﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using BusinessLogic.Controller;
using JScripter;
using DataEntity;

public partial class Admin_AjaxPages_CityAdd : AjaxPage
{
    private Guid ID
    {
        get
        {
            if (Request.Params["cid"]!=null || AjaxState.ContainsKey("cid"))
            {
                if (!AjaxState.ContainsKey("cid"))
                    AjaxState["cid"] = Request.Params["cid"];
                return new Guid(AjaxState["cid"]);
            }
            else
            {
                return Guid.Empty;
            }
        }
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        if (ID != Guid.Empty)
        {
            BindData();
        }
        else
        {
            new StateController().BindState(ddState);
        }
    }

    private void AddData()
    {
        try
        {
            City objCity = new City();

            objCity.CityID = Guid.NewGuid();

            objCity.CityName = HtmlHelper.ControlValue(txtCityName.ClientID);

            string chkval = HtmlHelper.ControlValue(chkIsGroup.ClientID);
            if (chkval == "")
                objCity.IsGroup = false;
            else
                objCity.IsGroup = Convert.ToBoolean(chkval);
            
            objCity.StateID=new Guid(HtmlHelper.ControlValue(ddState.ClientID));

            objCity.ModifiedDate = DateTime.Now;

            new CityController().Add(objCity);
            Response.Redirect("~/Admin/AjaxPages/CityList.aspx");
        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = "<div class='error'>" + ex.Message + "</div>";
        }
    }

    private void EditData()
    {
        Guid PID = ID;
        City objCity = new City();

        objCity.CityID = ID;

        objCity.CityName = HtmlHelper.ControlValue(txtCityName.ClientID);

        string chkval = HtmlHelper.ControlValue(chkIsGroup.ClientID);
        if (chkval == "")
            objCity.IsGroup = false;
        else
            objCity.IsGroup = Convert.ToBoolean(chkval);

        objCity.StateID=new Guid(HtmlHelper.ControlValue(ddState.ClientID));

        objCity.ModifiedDate = DateTime.Now;

        new CityController().UpdateByCityID(objCity);
        Response.Redirect("~/Admin/AjaxPages/CityList.aspx");
    }

    private void BindData()
    {
        var dataBunch = new CityController().GetbyCityID(ID);
        if (dataBunch.Count > 0)
        {
            var data = dataBunch[0];

            txtCityName.Text = data.CityName;

            new StateController().BindState(ddState, data.StateID.ToString());

            chkIsGroup.Checked = data.IsGroup.Value;



        }
    }

    protected void AddAjaxClick(object sender, AjaxControl.AjaxEventArg e)
    {
        AddData();
        
    }
    public void PreventData()
    {
        txtCityName.Text = HtmlHelper.ControlValue(txtCityName.ClientID);
        chkIsGroup.Checked = Convert.ToBoolean(HtmlHelper.ControlValue(chkIsGroup.ClientID));





    }
    protected void UpdateAjaxClick(object sender, AjaxControl.AjaxEventArg e)
    {
        EditData();
    }
}